package cn.nexttec.noteplayer.common

import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken

object GsonProvider {
    val gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
}

fun isPrimitiveType(obj: Any?): Boolean {
    return when (obj) {
        is Int, is Long, is Double, is Float,
        is Boolean, is Char, is Short, is Byte,
        is String -> true

        else -> false
    }
}

val gson = { any: Any -> GsonProvider.gson.toJson(any) }

fun Any.gson(): String = if (isPrimitiveType(this)) this.toString() else gson(this)
inline fun <reified T> String.parse(): T? {
    val type = object : TypeToken<T>() {}.type
    return GsonProvider.gson.fromJson(this, type)
}